package com.atguigu.lease.web.app.service.impl;


import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.BrowsingHistoryService;
import com.atguigu.lease.web.app.service.FacilityInfoService;
import com.atguigu.lease.web.app.service.LeaseAgreementService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrKeyVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.asn1.pkcs.RSAESOAEPparams;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
@Autowired
private ApartmentInfoMapper apartmentInfoMapper;
@Autowired
private GraphInfoMapper graphInfoMapper;
@Autowired
private AttrValueMapper attrValueMapper;
@Autowired
private FacilityInfoMapper facilityInfoMapper;
@Autowired
private LabelInfoMapper labelInfoMapper;
@Autowired
private PaymentTypeMapper paymentTypeMapper;
@Autowired
private FeeValueMapper feeValueMapper;
@Autowired
private LeaseTermMapper leaseTermMapper;
@Autowired
private RoomInfoMapper roomInfoMapper;
@Autowired
private LeaseAgreementMapper leaseAgreementMapper;

    @Override
    public void customPage(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        roomInfoMapper.customPage(page,queryVo);
    }

    @Override
    public RoomDetailVo customGetDetailById(Long id) {
        //1.先判断该房间id所对应房间是否已经被其他管理员删除
        RoomInfo roomInfo = getById(id);
        if (ObjectUtils.isEmpty(roomInfo)){
            return null;
        }
        //2.查询所属公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        List<LabelInfo> labelInfoList = labelInfoMapper.queryByapartId(roomInfo.getApartmentId());
        List<GraphInfo> graphInfoList = graphInfoMapper.queryByapartId(roomInfo.getApartmentId());
        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        BeanUtils.copyProperties(apartmentInfo,apartmentItemVo);
        apartmentItemVo.setGraphVoList(graphInfoList);
        apartmentItemVo.setLabelInfoList(labelInfoList);
        BigDecimal minRent = roomInfoMapper.findMinRentByapartId(roomInfo.getApartmentId());
        apartmentItemVo.setMinRent(minRent);

        //3.查询图片
        List<GraphVo> graphVoList = graphInfoMapper.queryVoByRoomId(id);
        //4.查询属性
        List<AttrValueVo> attrValueVoList = attrValueMapper.queryVoByRoomId(id);
        //5.查询配套信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.queryVoByRoomId(id);
        //6.查询标签信息列表
        List<LabelInfo> labelInfoList1 = labelInfoMapper.queryVoByRoomId(id);
        //7.查询支付方式列表
        List<PaymentType> paymentTypeList = paymentTypeMapper.queryVoByRoomId(id);
        //8.查询杂费列表
        List<FeeValueVo> feeValueVoList = feeValueMapper.queryVoByRoomId(roomInfo.getApartmentId());
        //9.查询租期列表
        List<LeaseTerm> leaseTermList = leaseTermMapper.queryVoByRoomId(id);
        //10查询是否已被删除
        LambdaQueryWrapper<RoomInfo> a = new LambdaQueryWrapper<>();
        a.eq(RoomInfo::getId,id);
        Byte isDeleted = roomInfoMapper.selectOne(a).getIsDeleted();
        boolean isDelete = isDeleted==1?true:false;

        //11查询房间是否已入住
        LambdaQueryWrapper<LeaseAgreement> c = new LambdaQueryWrapper<>();
        c.eq(LeaseAgreement::getRoomId,id).in(LeaseAgreement::getStatus,LeaseStatus.SIGNED,LeaseStatus.RENEWING,LeaseStatus.WITHDRAWING);
        c.eq(LeaseAgreement::getIsDeleted,0);
        boolean isCheckIn = leaseAgreementMapper.selectCount(c)==0?false:true;

        //12.把以上全捏到RoomDetailVo里
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        roomDetailVo.setIsCheckIn(isCheckIn);
        roomDetailVo.setIsDelete(isDelete);
        roomDetailVo.setApartmentItemVo(apartmentItemVo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList1);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setFeeValueVoList(feeValueVoList);
        roomDetailVo.setLeaseTermList(leaseTermList);

        return roomDetailVo;
    }

    @Override
    public void customPageByapartId(Page<RoomItemVo> page, Long id) {
        roomInfoMapper.customPageByapartId(page,id);
    }
}




